package com.tnt.springbootrabbmitmq.provider.directexchange;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Description 直连交换机消息生产者
 * @Author zhuangxl
 * @Date 2021-01-22 23:01
 */
@RestController
@RequestMapping("/direct/provider")
public class DirectExchangeProviderController {

    @Autowired
    AmqpTemplate amqpTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * @Description: 生产消息并发送到消息代理
     * @Author: zhuangxl
     * @Date: 21/1/22 23:09
     * @return: boolean
     **/
    @RequestMapping("/sendMessage")
    public Map<String, Object> sendMessage(){

        String messageId = UUID.randomUUID().toString();
        String messageContent = "I am Direct Exchange!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("messageId", messageId);
        paramsMap.put("messageContent", messageContent);
        paramsMap.put("createTime", createTime);

        // 发送到交换机，通过路由key找到对应的队列
        rabbitTemplate.convertAndSend("directExchange", "directRouting", paramsMap);

        return paramsMap;
    }
}
